//
//  YMRentFreeHeaderView.swift
//  RentFreeDemo
//
//  Created by youmy on 2018/11/23.
//  Copyright © 2018 youmy. All rights reserved.
//

import UIKit

fileprivate let hBundle = Bundle(for: YMRentFreeHeaderView.classForCoder())

protocol YMRentFreeHeaderViewDelegate {
    func arrowBtnDidClick(view:YMRentFreeHeaderView, btn:UIButton)
}

class YMRentFreeHeaderView: UIView {
    
    var delegate:YMRentFreeHeaderViewDelegate? {
        didSet{
            if delegate == nil {
                arrowIV.removeFromSuperview()
                arrowBtn.removeFromSuperview()
            }else{
                arrowIV.removeFromSuperview()
                arrowBtn.removeFromSuperview()
                addSubview(arrowIV)
                addSubview(arrowBtn)
            }
        }
    }
    
    public lazy var titleLb:UILabel = {
        let label = UILabel(frame: CGRect(x: 20, y: 15, width: ym_screen_width - 2*20, height: 15))
        label.font = UIFont(name: PingFangSC_Medium, size: 15)
        label.textColor = UIColor(valueRGB: 0x242424, alpha: 1)
        return label
    }()
    
    lazy var line:UIView = {
        let v = UIView(frame: CGRect(x: 0, y: height - 10, width: ym_screen_width, height: 0.35))
        v.backgroundColor = UIColor(valueRGB: 0xf0f0f0, alpha: 1)
        return v
    }()
    
    lazy var arrowIV:UIImageView = {
        let x = ym_screen_width - 12 - 20
        let imageV = UIImageView(frame: CGRect(x: x, y: 0, width: 12, height: 7))
        imageV.centerY = titleLb.centerY
        imageV.image = UIImage(named: "向下箭头", in: hBundle, compatibleWith: nil)
        return imageV
    }()
    
    lazy var arrowBtn:UIButton = {
        let btn = UIButton(frame: bounds)
        btn.addTarget(self, action: #selector(arrowBtnAction(sender:)), for: .touchDown)
        btn.isSelected = false
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(titleLb)
        addSubview(line)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func arrowBtnAction(sender:UIButton){
        sender.isSelected = !sender.isSelected
        
        arrowIV.image = UIImage(named: sender.isSelected ? "向上箭头" : "向下箭头", in: hBundle, compatibleWith: nil)
        
        delegate?.arrowBtnDidClick(view: self, btn: sender)
    }
}
